package ynu.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import ynu.edu.mapper.ICartMapper;
import ynu.edu.po.Cart;
import ynu.edu.service.ICartService;

import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    @Resource
    private ICartMapper cartMapper;

    @Override
    public List<Cart> listCart(Cart cart) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", cart.getUserId());
        if (cart.getBusinessId() != null) {
            queryWrapper.eq("businessId", cart.getBusinessId());
        }
        return cartMapper.selectList(queryWrapper);
    }

    @Override
    public boolean saveCart(Cart cart) {
        return cartMapper.insert(cart) > 0;
    }

    @Override
    public boolean updateCart(Cart cart) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", cart.getUserId())
                .eq("businessId", cart.getBusinessId())
                .eq("foodId", cart.getFoodId());
        return cartMapper.update(cart, queryWrapper) > 0;
    }


    @Override
    public boolean removeCart(Cart cart) {
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", cart.getUserId()).eq("businessId", cart.getBusinessId());
        if (cart.getFoodId() != null) {
            queryWrapper.eq("foodId", cart.getFoodId());
        }
        return cartMapper.delete(queryWrapper) > 0;
    }
}
